// 导航守卫：允许和不允许路由的通过以及路由的引导
// 导入路由

import { asyncRouter } from '@/router/asyncRouter'
import router from '@/router'
import store from '@/store'
import { getProfileApI } from '@/api/system'
import { Set } from 'core-js'
// 定义白名单：
const witeList = ['/login', '/register', '/404']

//处理一级路由函数
function getFirstMenuData(res) {
    const List = res.map(item => {
        return item.split(":")[0]
    })

    // const newList=[]
    // for(var i=0;i<List.length;i++){
    //   if(!newList.includes(List[i])){
    //     newList.path(List[i])
    //   }
    // }
    return [...new Set(List)]

    console.log(new Set(List))
}

//处理二级路由函数
function getSecondMenuData(res) {
    const List = res.map(item => {
        //parking:rule:list=>["parking","rule","list"]
        return item.split(":")[0] + ":" + item.split(":")[1]
    })

    // console.log(List)
    //去重返回数据
    return [...new Set(List)]
}

//匹配动态路由函数
function getRouterMatch(firstRouter, secondRouter, asyncRouter) {

    //处理超级管理员权限问题
    if (firstRouter.includes('*')) {
        return asyncRouter
    }

    // 先匹配一级路由，再在匹配到的一级路由的基础上匹配二级路由

    // 1.匹配一级路由
    const List1 = asyncRouter.filter(item => {
        return firstRouter.includes(item.permission)
    })

    console.log(List1)
    const List2 = List1.map(item1 => {
        return {
            ...item1,
            // secondRouter.includes()
            children: item1.children.filter(item2 => {
                return secondRouter.includes(item2.permission)
            })
        }
    })

    return List2
}

// 导航守卫
router.beforeEach(
    async(to, from, next) => {
        // to： 目标路由， 表示跳转到页面的路由
        // from:原路由
        // next:1、放行，2、引导跳转路由
        const token = store.state.user.token
        if (token) {
            if (to.path == '/login') {
                // 跳转到首页
                next('/')
                    // this.$router.push('/')
            } else {
                next() //next()没有参数代表路由放行

                // 1.获取用户的权限数据
                // const res = await getProfileApI()
                // console.log(res)
                const res = await store.dispatch('menu/getProfile')

                console.log(res)

                //2.处理数据['parking','park'] ['parking:rule',''park:rule']
                //2.1处理一级数据
                const firstMenuData = getFirstMenuData(res)

                // console.log(firstMenuData)

                // 处理二级路由数据

                const secondMenuData = getSecondMenuData(res)

                // console.log(secondMenuData)
                // 过滤权限路由
                const routerMatch = getRouterMatch(firstMenuData, secondMenuData, asyncRouter)

                console.log(routerMatch)

                // 把过滤好的权限路由添加到路由表中

                routerMatch.forEach(item => router.addRoute(item))

                // 提供一份路由给vuex
                store.commit('menu/setMenuRouterList', routerMatch)
            }
        } else {
            if (witeList.includes(to.path)) {
                next()
            } else {
                next('/login')
            }
        }
    }
)